<template>
  <el-row class="table-box">
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column v-if="selection"
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column v-if="serial"
        type="index"
        label="序号"
        align="center"
        width="65">
      </el-table-column>
      <el-table-column v-if="imageHead" :label="imageHead" align="center" width="100">
        <template slot-scope="scope">
          <slot :data="scope.row"></slot>
        </template>
      </el-table-column>
      <el-table-column v-for="(item, index) in thead" :key="index"
        :prop="item.prop"
        :label="item.name"
        :formatter="item.format"
        :width="item.width"
        :align="item.align || allAlign">
      </el-table-column>
      <el-table-column label="操作" v-if="options.length" :width="optWidth" :align="options.align || allAlign">
        <template slot-scope="scope">
          <template v-for="(item, index) in options">
            <el-button v-if="item.type!=='del'" @click="item.callback(scope.row, scope.$index)" type="text" size="small" :key="index">{{item.name}}</el-button>
            <el-popover :key="index" v-if="item.type==='del'"
              placement="top"
              width="160"
              v-model="scope.row.visible">
              <p>数据删除后不可找回，是否确定删除该数据？</p>
              <div style="text-align: right; margin: 0">
                <el-button size="mini" type="text" @click="scope.row.visible = false">取消</el-button>
                <el-button type="primary" size="mini" @click="item.callback(scope.row, scope.$index)">确定</el-button>
              </div>
              <el-button class="del-btn" slot="reference" type="text" size="small" style="margin-right: 16px">{{item.name}}</el-button>
            </el-popover>        
          </template>
        </template>
      </el-table-column>
    </el-table>
    <slot name="footer"></slot>
    <paginate v-if="pageData.callback" :callback="pageData.callback" :page="pageData.page"></paginate>
  </el-row>
</template>
<script>
export default {
  props: {
    tableData: { // 表单数据
      type: Array,
      default: function () {
        return []
      },
      required: true
    },
    thead: { // 表单头信息（prop:对应内容字段， name：标题， width：表格宽度, format：数据格式化, align：文字对齐方式）
      type: Array,
      default: function () {
        return []
      }
    },
    imageHead: String, // 头像
    serial: { // 序号
      type: Boolean,
      default: false
    },
    selection: { // 复选框
      type: Boolean,
      default: false
    },
    options: { // 操作选项
      type: Array,
      default: function () {
        return []
      }
    },
    allAlign: String, // 全部对齐方式
    optWidth: { // 操作宽度
      type: Number
    },
    pageData: { // 分页数据
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  methods: {
    handleSelectionChange (val) { // 选中数据（有复选框时）
      this.$emit('selectData', val)
    }
  }
}
</script>
<style lang="less" scoped>
.data-t {
    font-size: 14px;
  }
.table-box {
  margin-top: 15px;
}
.del-btn {
  margin-left: 12px;
}
</style>